#include "ResourceManager.h"

CResourceManager* CResourceManager::m_resource = NULL;
LPDIRECT3DDEVICE9 CResourceManager::m_pD3DDevice = NULL;

CResourceManager::CResourceManager()
{
	this->m_menuBackground = new CSprite(800, 600, 0, 0, 0, 800, 600, 0);
	this->m_menuGamePlay = new CSprite(800, 600, 0, 0, 0, 800, 600, 0);
	this->m_inputMouse = new CSprite(48, 48, 0, 0, 0, 48, 48, 0);
	m_gamePlayMarrioKid = new CSprite(128, 32, 0, 0, 0, 32, 32, 0);
	m_gamePlayEnemy = new CSprite(124, 32, 0, 0, 0, 31, 32, 0);
	m_gamePlayLand = new CSprite(500, 50, 0, 0, 0, 500, 50, 0);
	m_gamePlayWall = new CSprite(48, 48, 0, 0, 0, 48, 48, 0);
	m_gamePlayPipe = new CSprite(75, 100, 0, 0, 0, 75, 100, 0);
	m_gamePlayCoin = new CSprite(250, 100, 0, 0, 0, 50, 50, 0.1f);
	m_gamePlayMap = new CMap();
}

void CResourceManager::Init(LPDIRECT3DDEVICE9 pD3DDevice)
{
	this->m_pD3DDevice = pD3DDevice;
	this->m_menuBackground->Init(LoadTexture("..//Data//apple.jpg"));
	this->m_menuGamePlay->Init(LoadTexture("..//Data//colong.jpg"));
	this->m_inputMouse->Init(LoadTexture("..//Data//cursor.png"));
	m_gamePlayMarrioKid->Init(LoadTexture("..//Data//mario_kid.png"));
	m_gamePlayLand->Init(LoadTexture("..//Data//land.png"));
	m_gamePlayMap->LoadMapFromFile("..//Data//map.txt");
	m_gamePlayWall->Init(LoadTexture("..//Data//wall_green.png"));
	m_gamePlayPipe->Init(LoadTexture("..//Data//Pipe.png"));
	m_gamePlayCoin->Init(LoadTexture("..//Data//Gold.png"));
	m_gamePlayEnemy->Init(LoadTexture("..//Data//enemy.png"));
}

CSprite* CResourceManager::GetSprite(ResourceId id)
{
	switch (id)
	{
	case RESOURCE__MENU_BACKGROUND:
		return this->m_menuBackground;
		break;
	case RESOURCE__GAMEPLAY_BACKGROUND:
		return this->m_menuGamePlay;
		break;
	case RESOURCE__INPUT_CURSOR:
		return this->m_inputMouse;
		break;
	case RESOURCE__GAMEPLAY_MARIO_KID:
		return new CSprite(*m_gamePlayMarrioKid);
	case RESOURCE__GAMEPLAY_LAND:
		return new CSprite(*m_gamePlayLand);
	case RESOURCE__GAMEPLAY_WALL:
		return new CSprite(*m_gamePlayWall);
	case RESOURCE__GAMEPLAY_PIPE:
		return new CSprite(*m_gamePlayPipe);
	case RESOURCE__GAMEPLAY_COIN:
		return new CSprite(*m_gamePlayCoin);
	case RESOURCE__GAMEPLAY_ENEMY:
		return new CSprite(*m_gamePlayEnemy);
	default:
		return NULL;
		break;
	}
}

CMap* CResourceManager::GetMap(ResourceId id)
{
	switch (id)
	{
	case RESOURCE__GAMEPLAY_MAP:
		return m_gamePlayMap;
		break;
	default:
		return NULL;
		break;
	}
}
CResourceManager* CResourceManager::GetInstance()
{
	if (m_resource == NULL)
		m_resource = new CResourceManager();
	return m_resource;
}

LPDIRECT3DTEXTURE9 CResourceManager::LoadTexture(char* fileName)
{
	LPDIRECT3DTEXTURE9 texture;
	D3DXIMAGE_INFO fileInfo;
	HRESULT hResult;

	hResult = D3DXGetImageInfoFromFile(fileName, &fileInfo);
	if (hResult != D3D_OK)
	{
		MessageBox(NULL, "Load info file failed", "Error", MB_OK);
		return FALSE;
	}

	hResult = D3DXCreateTextureFromFileEx(this->m_pD3DDevice, fileName, fileInfo.Width, fileInfo.Height, 1, D3DUSAGE_DYNAMIC,
		D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_ARGB(255, 23, 255, 45), &fileInfo, NULL, &texture);
	if (hResult != D3D_OK)
	{
		MessageBox(NULL, "Load texture failed", "Error", MB_OK);
		return FALSE;
	}
	return texture;
}

